package com.example.cloudorder.web;

import com.example.cloudorder.entity.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
public class orderAction {

    @RequestMapping("order")
    public String order(HttpServletRequest request){
        int port = request.getServerPort();
        return "order :" + port;
    }

    @RequestMapping("hello")
    String hello(String name){
        return "Hello " + name;
    }

    @RequestMapping("add")
    int add(int a, int b){
        return a + b;
    }

    @RequestMapping("user")
    User user(@RequestBody User user){
        user.setId(user.getId() + 10);
        user.setName(user.getName() + "ok");
        return user;
    }

    @RequestMapping("foods")
    List<String> getFoods(){
        List<String> list = new ArrayList<String>();
        list.add("苹果");
        list.add("葡萄");
        list.add("香蕉");
        list.add("凤梨");
        list.add("菠萝");
        list.add("板栗");
        list.add("榴莲");
        list.add("火龙果");
        list.sort((a,b)->{
            return new Random().nextBoolean()?1:-1;
        });
        List<String> ret = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            ret.add(list.get(i));
        }
        return ret;
    }
}
